/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package snmpapplication.utils;

import snmpapplication.Main;
import snmpapplication.beans.ManagedDevice;
import snmpapplication.log.Severity;
import snmpapplication.snmp.SnmpClientGet;

/**
 * Class used to make periodical checks of the devices reachability
 * @author APetcu
 */
public class ReachabilityService extends Thread {

    private boolean running = true;

    @Override
    public void run() {

        while (running) {

            for (ManagedDevice md : DevicesManager.deviceList) {
                if (Ping.pingMe(md.getIpAddress().toString())) {
                    // reachable by ping
                    md.setIsReachable(Constante.PING_REACH);

                    // check SNMP access
                    if (SnmpClientGet.snmpGet(md.getIpAddress().toString(), "public", Constante.objId) != null) {
                        md.setIsReachable(Constante.SNMP_REACH);
                    }
                } else {
                    // not reachable
                    md.setIsReachable(Constante.NO_REACH);
                }
            }
            try {
                sleep(3000);
            } catch (InterruptedException ex) {
                Main.log.log(new Severity(Severity.SEV_ERR), "Reachability Service interupted exception " + ex.getMessage());
            }

        }
    }

    public boolean isRunning() {
        return running;
    }

    public void setRunning(boolean running) {
        this.running = running;
    }
}
